# -*- coding: utf-8 -*-
# coding:utf-8
# write  by  zhou

import gevent
import requests

from gevent.monkey import  patch_all;patch_all()
from celery import Celery

import sys
import ssl
context = ssl._create_unverified_context()
reload(sys)
sys.setdefaultencoding("utf-8")

app = Celery()
app.conf.task_ignore_result = True
app.conf.task_queue_max_priority = 255
import redis
redis = redis.Redis("192.168.8.40",db=1)

# @app.task(ignore_result=True)
def page_speed(url,callback_url,duration,timeout=3):

   '''
	必选参数
	url  要获取和分析的url
	可选参数
	 filter_third_party_resources               是否过滤掉但三方资源false 或true
	 locale                                     本地格式化语言   zh_CN
	 rule                                       要运行的规则    没有给出默认全选
     screenshot                                 指示是否包含截图二进制数据应包括（缺省：false）
     snapshots                                  指示是否包含快照图像的二进制数据应包括（缺省：false）
     strategy                                   要使用的分析策略（桌面或移动），而桌面是默认的 Acceptable值是：“ desktop”：获取并分析桌面浏览器的URL“ mobile”：获取并分析移动设备的URL
     utm_source                                 分析的广告系列来源
     utm_campaign                               分析的广告系列名称
	:return:
	'''
   print 'hello'
   if duration:
       duration = duration
       duration = max(30, int(duration), timeout)
   proxies = {'http': "http://199.247.18.222:9821", 'https': 'http://199.247.18.222:9821'}
   response = requests.get("https://www.googleapis.com/pagespeedonline/v4/runPagespeed",{
       'url': str(url),
       'locale':'zh_CN',
       'filter_third_party_resources': False,
       'strategy': 'desktop'
   }, proxies=proxies)
   # resp = requests.post(callback_url, {'data': response.text})
   # print resp.text
   print response.text
   return response.text



from celery import Celery
app = Celery(broker="redis://192.168.8.40:6379/1",backend='redis://192.168.8.40:6379/1')
if __name__ == '__main__':

    print app.send_task("celery_pagespeed.page_speed",
                        kwargs={
                            "url":"https://www.baidu.com/",
                            # 'callback_url':'http://192.168.8.40:8001/test_page/',
                            # 'duration':3000,
                            },
                        queue="celery_pagespeed.page_speed")
